<?php defined('SYSPATH') OR die('No direct Script Access');

class Controller_Grocerylist extends Controller_Securetemplate { 
	
	/**
	 * Index function. Will be executed when the controller is called.
	 */
	public function action_index() 
	{ 
		$this->template->title = 'Boodschappenlijst';
		$view = new View('pages/grocerylist');
		
		$model_grocery_list = new Model_Grocerylist();
		$grocery_list = $model_grocery_list->select_grocery_list($this->session->get('id'));
		
		$view->bind('grocery_list_id', $grocery_list['id']);
		$view->bind('grocery_list_name', $grocery_list['name']);
		//var_dump($grocery_list);
		if(isset($grocery_list['products']['products']))
		{
			$view->bind('products', $grocery_list['products']['products']);
		} 
		else
		{
			$view->bind('products', $grocery_list['products']);
		}
		
		$this->template->content = $view;
	}
	
	/**
	 * Function to delete a product from the grocerylist
	 */
	public function action_deletefromlist() 
	{	
		$this->auto_render = false;
		if(isset($_GET['id']))
		{
			$grocery_list_id = $this->session->get('grocery_list_id');
			$model_grocery_list = new Model_Grocerylist();
			$model_grocery_list->delete_from_list($grocery_list_id, $_GET['id'], 1);
		}
	}
	
	/**
	 * Function to change the amount you buy from one product
	 */
	public function action_changeproductamount()
	{
		if(isset($_GET['id']) && isset($_GET['amount'])) 
		{
			$grocery_list_id = $this->session->get('grocery_list_id');
			$model_grocery_list = new Model_Grocerylist();
			$model_grocery_list->change_product_amount($grocery_list_id, $_GET['id'], $_GET['amount']);
		}
	}
}
